package de.acagamics.roteschlupfmuetze.core;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.LinkedList;

import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.SpriteGroup;

import de.acagamics.roteschlupfmuetze.RoteSchlupfmuetze;
import de.acagamics.roteschlupfmuetze.data.LevelData;
import de.acagamics.roteschlupfmuetze.data.LevelTestData;
import de.acagamics.roteschlupfmuetze.input.InputManager;
import de.acagamics.roteschlupfmuetze.io.XMLDataReader;
import de.acagamics.roteschlupfmuetze.io.XMLDataWriter;
import de.acagamics.roteschlupfmuetze.level.GridGenerator;

public class Level {
	public static LevelData currentLevelData;
	
	LevelData levelData;
	Player player;
	Render render;
	InputManager input;
	LevelObject activeObject = null;
	LevelObject lastClick = null;
	boolean movingPlayer = false;

	boolean scriptEvent = false;
	public boolean isScriptEvent() { return scriptEvent; }
	boolean levelStartScriptUsed = false;
	int levelStartScriptProgress = 0;
	
	BufferedImage tile = null;

	public Level(String level) {
		player = new Player();
		render = new Render(player.getPlayerSprites());
		tile = RoteSchlupfmuetze
				.loadImage("de/acagamics/roteschlupfmuetze/dummy/tile_sample.png");
		loadLevel(level);	
		input = new InputManager(RoteSchlupfmuetze.game);
	}
	
	public Level(int level) {
		player = new Player();
		render = new Render(player.getPlayerSprites());
		tile = RoteSchlupfmuetze
				.loadImage("de/acagamics/roteschlupfmuetze/dummy/tile_sample.png");
		loadLevel(level);	
		input = new InputManager(RoteSchlupfmuetze.game);
	}

	private void loadLevel(int level) {
		LevelTestData lvt = new LevelTestData();
		
		switch(level) {
		case -1: this.levelData = lvt.getCredits();break;
		case 0: this.levelData = lvt.getIntro();break;
		case 1: this.levelData = lvt.getLevel1();break;
		case 2: this.levelData = lvt.getLevel2();break;
		case 3: this.levelData = lvt.getLevel3();break;
		case 4: this.levelData = lvt.getLevel4();break;
		}
		Level.currentLevelData = this.levelData;
		this.levelData.wasLoaded();
		init();
	}

	private void init() {
		render.reset();
		player.resetInventory();

		if(levelData.getLevelNumber() == -1) scriptEvent = true;
		if(levelData.getLevelNumber() == 0) {
			player.addItem(levelData.getObjects().get(1));
			levelData.getObjects().get(1).setZimage(levelData.getObjects().get(1).getImage_inventory());
			levelData.getObjects().get(1).setPosX(0);
			levelData.getObjects().get(1).setPosY((player.getInventorySize()-1)*80);
		}
		render.loadLevel(levelData);
		Sprite playerSprite = player.getPlayerSprite();
		playerSprite.forceX(-(5*playerSprite.getWidth()));
		playerSprite.forceY(levelData.getPitOffsetY()
				- GridGenerator.getPitHeight(levelData.getPitSizeY())
				- playerSprite.getHeight() - 5);
		this.levelStartScriptUsed = false;
	}

	public void loadLevel(String level) {
		/*LevelTestData lvt = new LevelTestData();
    	XMLDataWriter<LevelData> writer = new XMLDataWriter<LevelData>();
    	writer.WriteXMLFile(
    	"resources/de/acagamics/roteschlupfmuetze/res/level/introLevel.ser.xml",
    	lvt.getIntro());*/
    	
		/*LevelTestData lvt2 = new LevelTestData();
    	writer.WriteXMLFile(
    	"resources/de/acagamics/roteschlupfmuetze/res/level/testLevel2.ser.xml",
    	lvt2.getTestLevel2());
    	
		LevelTestData lvt3 = new LevelTestData();
    	writer.WriteXMLFile(
    	"resources/de/acagamics/roteschlupfmuetze/res/level/testLevel3.ser.xml",
    	lvt3.getTestLevel3());*/
    	
		XMLDataReader<LevelData> reader = new XMLDataReader<LevelData>();

		/*this.levelData = reader.readXMLData(level);
		XMLDataWriter<LevelData> writer = new XMLDataWriter<LevelData>();
    	writer.WriteXMLFile(
    	"resources/de/acagamics/roteschlupfmuetze/res/level/Level3.ser.xml",
    	this.levelData);*/
		Level.currentLevelData = this.levelData;
		this.levelData.wasLoaded();
		init();
	}

	public void update(long elapsedTime) {
		if(!levelStartScriptUsed && levelData.getLevelNumber() != -1)
			levelStartScript();
		if(this.levelData.getLevelNumber() == 0) {
			this.intro();
		}
		if(this.levelData.getLevelNumber() == -1) {
			this.credits(elapsedTime);
		}
		if(level1endScript){
			levelOne();
		}
		if(level2animation)
		{
			levelTwo();
		}
		if(level3animation)
		{
			levelThree();
		}
		if(level4animation)
		{
			levelFour();
		}

		
		updateInput();
		player.update(elapsedTime);
		render.update(elapsedTime);
		if(PlayerTileCollision.isCollided) {
			player.stopPlayer(true);
			PlayerTileCollision.reset();
		}
	}

	private void updateInput() {
		if(!scriptEvent) input.update();
		
		if (lastClick != null && lastClick.getObjectSprite().getWidth() <= 100 &&
				Math.abs(player.getPlayerSprite().getX() + player.getPlayerSprite().getWidth()/2 - lastClick.getObjectSprite().getX()) < 80
				&& movingPlayer){
			clickAction_reachable(lastClick);
		} else if (lastClick != null &&
				Math.abs(player.getPlayerSprite().getX() + player.getPlayerSprite().getWidth()/2 - lastClick.getObjectSprite().getX()) < 200
				&& movingPlayer){
			clickAction_reachable(lastClick);
		}

		if (input.wasClicked()) {

			// search for clicked on LevelObjects
			for (LevelObject el : levelData.getObjects()) {
				if (el.isClickable()) {
					if (el.getObjectSprite().getX() <= input.getMouseX()
							&& el.getObjectSprite().getX() + el.getObjectSprite().getWidth() >= input
									.getMouseX()
							&& el.getObjectSprite().getY() <= input.getMouseY()
							&& el.getObjectSprite().getY() + el.getObjectSprite().getHeight() >= input
									.getMouseY()) {
						movingPlayer = true;
						clickAction_unreachable(el);
						System.out.println("clicked");
						lastClick = el;
					}
				}
			}
			// move player to mousePosition
			if(input.getMouseX() > 90) {
				if(level2cageIsDropped || level3cageIsDropped) {
					if(input.getMouseX() < levelData.getObjects().get(1).getObjectSprite().getX()) {
						player.moveToX((int)levelData.getObjects().get(1).getObjectSprite().getX());
					}else if(input.getMouseX() > levelData.getObjects().get(1).getObjectSprite().getX() + levelData.getObjects().get(1).getObjectSprite().getWidth()) {
						player.moveToX((int)levelData.getObjects().get(1).getObjectSprite().getX() + levelData.getObjects().get(1).getObjectSprite().getWidth());
					}else
						player.moveToX(input.getMouseX());
				}else{
					if(input.getMouseX() < levelData.getPitOffsetX()) {
						player.moveToX((int)levelData.getPitOffsetX());
					}else if(input.getMouseX() > levelData.getPitOffsetX() + (levelData.getPitSizeX() * LevelData.TILE_SIZE_X)){
						player.moveToX((int)(levelData.getPitOffsetX() + (levelData.getPitSizeX() * LevelData.TILE_SIZE_X)));
					}else
						player.moveToX(input.getMouseX());
				}
			}
		}
	}
	
	public void clickAction_unreachable(LevelObject object){
		
		//laden
		if (object.getClickAction().matches("load")){
			//loadLevel("resources/de/acagamics/roteschlupfmuetze/res/level/Level1.ser.xml");
			//movingPlayer = false;
		}
		
		
		//select item from inventory
		if (object.getClickAction().matches("combine") &&
				activeObject==null){
			activeObject = object;
			activeObject.setPosX(40);
			movingPlayer = false;
		}		
		//deselect item from inventory
		else if (object.getClickAction().matches("combine") &&
				activeObject!=null){
			activeObject.setPosX(0);
			activeObject = null;
			movingPlayer = false;
		}
		
	}
	
	public void clickAction_reachable(LevelObject object){

		movingPlayer = false;
		
		

		//neues level
		/*if (object.getClickAction().matches("used")){
			//loadLevel("resources/de/acagamics/roteschlupfmuetze/res/level/testLevel2.ser.xml");
		}*/
		if (activeObject!=null && object.getClickAction().matches("destroy")){
			level4build = true;
			player.useItem(activeObject);
			activeObject = null;
			System.out.println("destroy");
			level4destroy = true;
		}
		
		if (object.getClickAction().matches("build")){
			level4destroy = false;
			level4build = true;
		}
		
		//activate object action
		if (object.getClickAction().matches("action")){
			switch(this.levelData.getLevelNumber()) {
			case 1: 	ropeActive1 = true;
			levelOne(); break;
			case 2: levelTwo(); break;

			case 3: levelThree(); break;

			case 4: levelFour(); break;

			}
		}
		
		//combine inventory object with world object
		if (object.getClickAction().matches("combinable") &&
				activeObject!=null){
			
			switch(this.levelData.getLevelNumber()) {
			case 1: levelOne(); break;
			case 2: levelTwo(); break;

			case 3: levelThree(); break;

			case 4: levelFour(); break;

			default:
				activeObject.setPosX(object.getPosX()-50);
				activeObject.setPosY(object.getPosY());
				player.useItem(activeObject);
				activeObject.setClickAction("used");
				activeObject = null;break;
			}
		}
		
		//pick up clicked on object
		if (object.getClickAction().matches("inventar")){
			object.setClickAction("combine");
			player.addItem(object);
			object.setHeight(80);
			object.setWidth(80);
			object.setZimage(object.getImage_inventory());
			object.setPosX(0);
			object.setPosY((player.getInventorySize()-1)*80);//put into right position in inventory
			System.out.println("inventar");
		}
	}

	public void render(Graphics2D g) {
		render.render(g);
	}
	
	private void levelStartScript() {
		scriptEvent = true;
		Sprite playerSprite = player.getPlayerSprite();
		int targetYPosition = (int)playerSprite.getY();
		if (levelData.getDrop()) {
			targetYPosition = levelData.fromLevelY(0) - playerSprite.getHeight();
		}
		
		player.moveToX(levelData.getPitOffsetX() + playerSprite.getWidth());
		
		if(playerSprite.getX() >= levelData.getPitOffsetX() && levelData.getDrop()) {
			if (levelStartScriptProgress == 0) {
				Render.render.move();
				levelStartScriptProgress = 1;
				player.moveToX((int)playerSprite.getX() - 3);
			} else if (levelStartScriptProgress == 20) {
				player.moveToY(levelData.getPitOffsetY() - playerSprite.getHeight());
			} else {
				levelStartScriptProgress++;
				player.moveToX((int)playerSprite.getX()
						+ ((levelStartScriptProgress < 10) ? -3 : 3));
			}
		}
		
		if(playerSprite.getX() >= levelData.getPitOffsetX()
				&& playerSprite.getY()  >= targetYPosition - (playerSprite.getHeight() + 20)) {
			levelStartScriptUsed = true;
			scriptEvent = false;
			levelStartScriptProgress = 0;
		}
	}
	
	int time = 0;
	private void credits(long elapsedTime) {
		time += elapsedTime;
		Sprite o1, o2, o3, o4;
		int step = time / 200;
		LinkedList<LevelObject> credit = levelData.getObjects();
		o1 = credit.get(1).getObjectSprite();
		o2 = credit.get(2).getObjectSprite();
		o3 = credit.get(3).getObjectSprite();
		o4 = credit.get(4).getObjectSprite();
		int pos = 400;
		
		if (step < 1) {
			render.move();
		} else if (step >= 5 && step <= 14) {
			o1.forceX(pos);
		} else if (step >= 15 && step <= 24) {
			o1.forceX(RoteSchlupfmuetze.WIDTH);
			o2 = credit.get(2).getObjectSprite();
			o2.forceX(pos);
		} else if (step >= 25 && step <= 34) {
			o2.forceX(RoteSchlupfmuetze.WIDTH);
			o3 = credit.get(3).getObjectSprite();
			o3.forceX(pos+45);
			o3.forceY(o3.getY());
		} else if (step >= 35 && step <= 44) {
			o3.forceX(RoteSchlupfmuetze.WIDTH);
			o4 = credit.get(4).getObjectSprite();
			o4.forceX(pos+25);
		} else if (step >= 45) {
			loadLevel(0);
		}
	}
	
	boolean introWolve1 = true;
	int introWolve2 = 0;
	int introWolve3 = 0;
	int wolfLeftTurnProgress = 100;
	int wolfRightTurnProgress = 1;
	boolean introWolve4 = false;
	boolean introWolve5 = false;
	boolean introChild1 = false;
	boolean introChild2 = false;
	int introChildJumpeight = 60;
	int introChildCurrentJumpHeight = 0;
	int introChildJumpCount = 0;
	int introChildMaxJumpCount = 3;
	private void intro() {
		scriptEvent = true;
		LevelObject wolve = this.levelData.getObjects().get(0);
		if(wolve.getPosX()>this.levelData.getPitOffsetX()+player.getPlayerSprite().getWidth() && introWolve1) {
			wolve.getObjectSprite().moveX(-2);
		}else if(introWolve1) {
			introWolve1 = false;
		}else if(introWolve2 < 5) {
			wolve.setHeight(wolve.getObjectSprite().getHeight() + 2);
			wolve.setZimage(wolve.getZimage());
			wolve.getObjectSprite().moveY(-0.2);
			introWolve2++;
		}else if(introWolve3 < 5) {
			wolve.setHeight(wolve.getObjectSprite().getHeight() - 2);
			wolve.setZimage("de/acagamics/roteschlupfmuetze/special/special_wolve_left_basket.png");
			player.resetInventory();
			wolve.getObjectSprite().moveY(0.2);
			introWolve4 = true;
			introWolve3++;
		}else if (wolfLeftTurnProgress > 1) {
			wolve.setWidth(wolve.getObjectSprite().getWidth() - 8);
			wolve.setZimage("de/acagamics/roteschlupfmuetze/special/special_wolve_left_basket.png");
			wolve.getObjectSprite().moveX(0.5);
			wolfLeftTurnProgress -= 4;
		}else if(introWolve4) {
			wolve.setZimage("de/acagamics/roteschlupfmuetze/special/special_wolve_right_basket.png");
			introWolve4 = false;
			introWolve5 = true;
		}else if (wolfRightTurnProgress < 100) {
			wolve.setWidth(wolve.getObjectSprite().getWidth() + 8);
			wolve.setZimage("de/acagamics/roteschlupfmuetze/special/special_wolve_right_basket.png");
			wolve.getObjectSprite().moveX(0.5);
			wolfRightTurnProgress += 4;
		}else if(introWolve4 && wolve.getPosX() < player.getPlayerSprite().getX() + player.getPlayerSprite().getWidth() + 50) {
			wolve.getObjectSprite().moveX(+2);
		}else if(introWolve5 && wolve.getPosX() < RoteSchlupfmuetze.WIDTH) {
			wolve.getObjectSprite().moveX(+4);
		}else if(introWolve5){
			wolve.getObjectSprite().setActive(false);
			introWolve5 = false;
			introChild2 = true;
		}/*else if(introChild1 && introChildJumpCount < introChildMaxJumpCount
				&& player.getPlayerSprite().getY() < levelData.fromLevelY(0) - player.getPlayerSprite().getHeight() - introChildJumpeight) {
			player.moveToY(levelData.fromLevelY(0) - player.getPlayerSprite().getHeight() - introChildJumpeight);
		}else if(introChild1 && introChildJumpCount < introChildMaxJumpCount
				&& player.getPlayerSprite().getY() > levelData.fromLevelY(0) - player.getPlayerSprite().getHeight()) {
			introChildJumpCount++;
			player.moveToY(levelData.fromLevelY(0) - player.getPlayerSprite().getHeight());
		}else if(introChild1) {
			introChild1 = false;
			introChild2 = true;
		}*/else if(introChild2 && player.getPlayerSprite().getX() < RoteSchlupfmuetze.WIDTH) {
			player.moveToX(RoteSchlupfmuetze.WIDTH + 20,true);
		}else if(introChild2) {
			introChild2 = false;
		}else{
			scriptEvent = false;
			loadLevel(1);
		}
	}
	boolean ropeActive1 = false;
	boolean climbA1 = false;
	boolean climbB1 = false;
	boolean climbC1 = false;
	boolean climbD1 = false;
	boolean climbE1 = false;
	boolean level1endScript = false;
	private void levelOne() {
		System.out.println(player.getPlayerSprite().getX());
		if (!ropeActive1 && !level1endScript){
			levelData.getObjects().getLast().setVisible(true);
			levelData.getObjects().getLast().setClickable(true);
			player.useItem(activeObject);
			activeObject.setClickAction("used");
			activeObject = null;
		}
		if (ropeActive1  && !level1endScript){
			climbA1 = true;
		}
		if (climbA1){
			player.moveToX(730, false);
			level1endScript = true;
			scriptEvent = true;
			climbB1 = true;
			climbA1 = false;
		} else if (climbB1 && player.getPlayerSprite().getY() >= 140){
			player.moveToY(125);
			climbC1 = true;
		}  else if (climbC1 && player.getPlayerSprite().getX() >= 600){
			player.moveToX(500);
			climbD1 = true;
		} else if (climbD1 && player.getPlayerSprite().getX()<=RoteSchlupfmuetze.WIDTH+50){
			player.moveToX(RoteSchlupfmuetze.WIDTH + 100, true);
			player.moveToY(85);
			climbC1 = false;
		} else if (climbD1){//climbE1){
			scriptEvent = false;
			level1endScript = false;
			loadLevel(2);
		}

	}
	
	boolean level2ropeActive = false;
	boolean level2droppingChest = false;
	boolean level2moveUnderChest = false;
	boolean level2firstAction = true;
	boolean level2animation = false;
	boolean level2endingScript = false;
	boolean level2chestToCage = false;
	boolean level2dropCage = false;
	boolean level2buttonPressed = false;
	boolean level2cageIsDropped = false;
	boolean level2moveChestOut = false;
	boolean level2ropeToGras = false;
	boolean level2finalRope = false;
	boolean level2endAnimationFirstStep = false;
	boolean level2endAnimationSecondStep = false;
	private void levelTwo() {
		//double pX = player.getPlayerSprite().getX();
		if (!level2ropeActive){
			if(activeObject != null && activeObject.getName() == "rope"
					&& lastClick != null && lastClick.getName() == "crate")
			{
				levelData.getObjects().getLast().setVisible(true);
				levelData.getObjects().getLast().setClickable(true);
				player.useItem(activeObject);
				activeObject.setClickAction("used");
				activeObject = null;
				level2ropeActive = true;
			}
		}else if (level2ropeActive && level2firstAction){
			if(lastClick.getName() == "rope_climp") {
				level2droppingChest = true;
				level2firstAction = false;
				level2moveUnderChest = true;
				lastClick.setVisible(false);
				lastClick.setClickable(false);
				levelData.getObjects().get(0).setZimage("de/acagamics/roteschlupfmuetze/objects/objects_rope.png");
				levelData.getObjects().get(0).setPosX(levelData.getPitOffsetX() + 270);
				levelData.getObjects().get(0).setPosY(levelData.getPitOffsetY() - 30);
				levelData.getObjects().get(0).scaleImage(0.5);
				levelData.getObjects().get(0).setVisible(true);
				levelData.getObjects().get(0).setClickAction("inventar");
				scriptEvent = true;
				level2animation = true;
			}
		}else if(level2droppingChest && level2moveUnderChest
				&& (player.getPlayerSprite().getX() > ((levelData.getObjects().get(1).getObjectSprite().getX() + (levelData.getObjects().get(1).getObjectSprite().getWidth())/2)) + 20
				|| player.getPlayerSprite().getX() < ((levelData.getObjects().get(1).getObjectSprite().getX() + (levelData.getObjects().get(1).getObjectSprite().getWidth())/2)) - 20)){
				//&& (pX > levelData.getPitOffsetX() + 50 + 20 
				//|| pX < levelData.getPitOffsetX() + 50 - 20 )) {
			player.moveToX((int)(levelData.getObjects().get(1).getObjectSprite().getX() + (levelData.getObjects().get(1).getObjectSprite().getWidth()/2)));
			//player.moveToX(levelData.getPitOffsetX() + 50 );
		}else if(level2droppingChest && level2moveUnderChest) {
			level2moveUnderChest = false;
			level2chestToCage = true;
		}else if(level2droppingChest && level2chestToCage) {
			levelData.getObjects().get(1).setWidth(280);
			levelData.getObjects().get(1).setHeight(200);
			levelData.getObjects().get(1).setZimage("de/acagamics/roteschlupfmuetze/objects/objects_cage.png");
			levelData.getObjects().get(1).setPosX(levelData.getObjects().get(1).getPosX()-20);
			levelData.getObjects().get(1).setClickable(false);
			level2chestToCage = false;
			level2dropCage = true;
		}else if(level2droppingChest && level2dropCage
				&& levelData.getObjects().get(1).getObjectSprite().getY() < levelData.getPitOffsetY() - levelData.getObjects().get(1).getObjectSprite().getHeight()){
			levelData.getObjects().get(1).getObjectSprite().moveY(+15);
		}else if(level2droppingChest && level2dropCage){
			level2dropCage = false;
			level2droppingChest = false;
			level2animation = false;
			scriptEvent = false;
			level2cageIsDropped = true;
			levelData.getObjects().get(2).setClickable(true);
		}else if(level2cageIsDropped && !level2buttonPressed) {
			if(lastClick.getName() == "button") {
				level2buttonPressed = true;
				level2moveChestOut = true;
				level2animation = true;
				level2cageIsDropped = false;
				levelData.getObjects().get(2).setClickable(false);
				levelData.getObjects().get(3).setVisible(true);
			}
		}else if(level2buttonPressed && level2moveChestOut && levelData.getObjects().get(1).getObjectSprite().getX() < RoteSchlupfmuetze.WIDTH){
			levelData.getObjects().get(1).getObjectSprite().moveX(50);
		}else if(level2buttonPressed && level2moveChestOut) {
			level2moveChestOut = false;
			level2animation = false;
			level2ropeToGras = true;
		}else if(level2ropeToGras){
			if(lastClick.getName() == "gras"){
				levelData.getObjects().getLast().setVisible(true);
				levelData.getObjects().getLast().setClickable(true);
				levelData.getObjects().getLast().getObjectSprite().setX(levelData.getObjects().get(3).getObjectSprite().getX());
				levelData.getObjects().getLast().getObjectSprite().setY(levelData.getObjects().get(3).getObjectSprite().getY());
				levelData.getObjects().getLast().setHeight(500);
				levelData.getObjects().getLast().setZimage("de/acagamics/roteschlupfmuetze/objects/objects_rope_hooked_right.png");
				player.useItem(activeObject);
				activeObject.setClickAction("used");
				activeObject = null;
				level2ropeToGras = false;
				level2finalRope = true;
			}
		}else if(level2finalRope) {
			level2animation = true;
			scriptEvent = true;
			level2endAnimationFirstStep = true;
			level2finalRope = false;
		}else if(level2endAnimationFirstStep && player.getPlayerSprite().getY() > levelData.getPitOffsetY()
				- GridGenerator.getPitHeight(levelData.getPitSizeY())
				- player.getPlayerSprite().getHeight() - 10) {
			player.moveToY(levelData.getPitOffsetY()
				- GridGenerator.getPitHeight(levelData.getPitSizeY())
				- player.getPlayerSprite().getHeight() - 20);
		}else if(level2endAnimationFirstStep) {
			level2endAnimationFirstStep = false;
			level2endAnimationSecondStep = true;
		}else if(level2endAnimationSecondStep && player.getPlayerSprite().getX() < RoteSchlupfmuetze.WIDTH ) {
			player.moveToX(RoteSchlupfmuetze.WIDTH + 40, true);
		}else if(level2endAnimationSecondStep) {
			level2endAnimationSecondStep = false;
			level2animation = false;
			scriptEvent = false;
			loadLevel(3);
		}
		
	}
	
	boolean level3ropeActive = false;
	boolean level3droppingChest = false;
	boolean level3moveUnderChest = false;
	boolean level3firstAction = true;
	boolean level3animation = false;
	boolean level3endingScript = false;
	boolean level3chestToCage = false;
	boolean level3dropCage = false;
	boolean level3buttonPressed = false;
	boolean level3cageIsDropped = false;
	boolean level3moveChestOut = false;
	boolean level3ropeToGras = false;
	boolean level3finalRope = false;
	boolean level3dropGras = false;
	boolean level3grasIsDropping = false;
	boolean level3searchBunny = false;
	boolean level3bunnyActive = false;
	boolean level3endAnimationFirstStep = false;
	boolean level3endAnimationSecondStep = false;
	private void levelThree(){
		if (!level3ropeActive){
			if(activeObject != null && activeObject.getName() == "rope"
					&& lastClick != null && lastClick.getName() == "crate")
			{
				levelData.getObjects().getLast().setVisible(true);
				levelData.getObjects().getLast().setClickable(true);
				player.useItem(activeObject);
				activeObject.setClickAction("used");
				activeObject = null;
				level3ropeActive = true;
			}
		}else if (level3ropeActive && level3firstAction){
			if(lastClick.getName() == "rope_climp") {
				level3droppingChest = true;
				level3firstAction = false;
				level3moveUnderChest = true;
				lastClick.setVisible(false);
				lastClick.setClickable(false);
				levelData.getObjects().get(0).setZimage("de/acagamics/roteschlupfmuetze/objects/objects_rope.png");
				levelData.getObjects().get(0).setPosX(levelData.getPitOffsetX() + 270);
				levelData.getObjects().get(0).setPosY(levelData.getPitOffsetY() - 30);
				levelData.getObjects().get(0).scaleImage(0.5);
				levelData.getObjects().get(0).setVisible(true);
				levelData.getObjects().get(0).setClickAction("inventar");
				scriptEvent = true;
				level3animation = true;
			}
		}else if(level3droppingChest && level3moveUnderChest
				&& (player.getPlayerSprite().getX() > ((levelData.getObjects().get(1).getObjectSprite().getX() + (levelData.getObjects().get(1).getObjectSprite().getWidth())/2)) + 20
				|| player.getPlayerSprite().getX() < ((levelData.getObjects().get(1).getObjectSprite().getX() + (levelData.getObjects().get(1).getObjectSprite().getWidth())/2)) - 20)){
				//&& (pX > levelData.getPitOffsetX() + 50 + 20 
				//|| pX < levelData.getPitOffsetX() + 50 - 20 )) {
			player.moveToX((int)(levelData.getObjects().get(1).getObjectSprite().getX() + (levelData.getObjects().get(1).getObjectSprite().getWidth()/2)));
			//player.moveToX(levelData.getPitOffsetX() + 50 );
		}else if(level3droppingChest && level3moveUnderChest) {
			level3moveUnderChest = false;
			level3chestToCage = true;
		}else if(level3droppingChest && level3chestToCage) {
			levelData.getObjects().get(1).setWidth(280);
			levelData.getObjects().get(1).setHeight(200);
			levelData.getObjects().get(1).setZimage("de/acagamics/roteschlupfmuetze/objects/objects_cage.png");
			levelData.getObjects().get(1).setPosX(levelData.getObjects().get(1).getPosX()-20);
			levelData.getObjects().get(1).setClickable(false);
			level3chestToCage = false;
			level3dropCage = true;
		}else if(level3droppingChest && level3dropCage
				&& levelData.getObjects().get(1).getObjectSprite().getY() < levelData.getPitOffsetY() - levelData.getObjects().get(1).getObjectSprite().getHeight()){
			levelData.getObjects().get(1).getObjectSprite().moveY(+15);
		}else if(level3droppingChest && level3dropCage){
			level3dropCage = false;
			level3droppingChest = false;
			level3animation = false;
			scriptEvent = false;
			level3cageIsDropped = true;
			levelData.getObjects().get(2).setClickable(true);
		}else if(level3cageIsDropped && !level3buttonPressed) {
			if(lastClick.getName() == "button") {
				level3buttonPressed = true;
				level3moveChestOut = true;
				level3animation = true;
				level3cageIsDropped = false;
				levelData.getObjects().get(2).setClickable(false);
				levelData.getObjects().get(3).setVisible(true);
			}
		}else if(level3buttonPressed && level3moveChestOut && levelData.getObjects().get(1).getObjectSprite().getX() < RoteSchlupfmuetze.WIDTH){
			levelData.getObjects().get(1).getObjectSprite().moveX(50);
		}else if(level3buttonPressed && level3moveChestOut) {
			level3moveChestOut = false;
			level3animation = false;
			level3ropeToGras = true;
		}else if(level3ropeToGras){
			if(lastClick.getName() == "gras"){
				levelData.getObjects().getLast().setVisible(true);
				levelData.getObjects().getLast().setClickable(true);
				levelData.getObjects().getLast().getObjectSprite().setX(levelData.getObjects().get(3).getObjectSprite().getX());
				levelData.getObjects().getLast().getObjectSprite().setY(levelData.getObjects().get(3).getObjectSprite().getY());
				levelData.getObjects().getLast().setHeight(500);
				levelData.getObjects().getLast().setZimage("de/acagamics/roteschlupfmuetze/objects/objects_rope_hooked_right.png");
				player.useItem(activeObject);
				activeObject.setClickAction("used");
				activeObject = null;
				level3ropeToGras = false;
				level3finalRope = true;
			}
		}else if(level3finalRope) {
			level3animation = true;
			scriptEvent = true;
			level3dropGras = true;
			level3finalRope = false;
		}else if(level3dropGras) {
			levelData.getObjects().getLast().setClickable(false);
			levelData.getObjects().getLast().setVisible(false);
			level3dropGras = false;
			level3animation = true;
			level3grasIsDropping = true;
		}else if(level3grasIsDropping && levelData.getObjects().get(3).getObjectSprite().getY() < levelData.getPitOffsetY() - 40) {
			levelData.getObjects().get(3).getObjectSprite().moveX(-3);
			levelData.getObjects().get(3).getObjectSprite().moveY(+30);
		}else if(level3grasIsDropping) {
			level3grasIsDropping = false;
			levelData.getObjects().get(4).setClickable(true);
			levelData.getObjects().get(3).setClickAction("inventar");
			level3animation = false;
			scriptEvent = false;
			level3searchBunny = true;
		}else if(level3searchBunny) {
			if(activeObject != null && activeObject.getName() == "gras" 
					&& lastClick != null && lastClick.getName() == "bunny"){
				lastClick.getObjectSprite().moveX(-50);
				player.useItem(activeObject);
				activeObject.setClickAction("used");
				activeObject = null;
				level3searchBunny = false;
				level3bunnyActive = true;
			}
		}else if(level3bunnyActive){
			if(lastClick.getName() == "bunny"){
				level3endAnimationFirstStep = true;
				level3animation = true;
				scriptEvent = true;
				level3bunnyActive = false;
			}
		}else if(level3endAnimationFirstStep && player.getPlayerSprite().getY() > levelData.getPitOffsetY()
				- GridGenerator.getPitHeight(levelData.getPitSizeY())
				- player.getPlayerSprite().getHeight() - 10) {
			player.moveToY(levelData.getPitOffsetY()
				- GridGenerator.getPitHeight(levelData.getPitSizeY())
				- player.getPlayerSprite().getHeight() - 20);
			levelData.getObjects().get(4).getObjectSprite().moveY(-player.getMovementSpeedY());
		}else if(level3endAnimationFirstStep) {
			level3endAnimationFirstStep = false;
			level3endAnimationSecondStep = true;
		}else if(level3endAnimationSecondStep && player.getPlayerSprite().getX() < RoteSchlupfmuetze.WIDTH ) {
			player.moveToX(RoteSchlupfmuetze.WIDTH + 40, true);
			levelData.getObjects().get(4).getObjectSprite().moveX(4);
		}else if(level3endAnimationSecondStep) {
			level3endAnimationSecondStep = false;
			level3animation = false;
			scriptEvent = false;
			loadLevel(4);
		}
	}
	
	boolean level4ropeActive = false;
	boolean level4droppingChest = false;
	boolean level4moveUnderChest = false;
	boolean level4firstAction = true;
	boolean level4animation = false;
	boolean level4endingScript = false;
	boolean level4chestToCage = false;
	boolean level4dropCage = false;
	boolean level4buttonPressed = false;
	boolean level4cageIsDropped = false;
	boolean level4destroy = false;
	boolean level4build = false;
	boolean level4end = false;
	
	private void levelFour(){
		//double pX = player.getPlayerSprite().getX();
		if (!level4ropeActive){
			levelData.getObjects().getLast().setVisible(true);
			levelData.getObjects().getLast().setClickable(true);
			player.useItem(activeObject);
			activeObject.setClickAction("used");
			activeObject = null;
			level4ropeActive = true;
		}else if (level4ropeActive && level4firstAction){
			if(lastClick.getName() == "rope_climp") {
				level4droppingChest = true;
				level4firstAction = false;
				level4moveUnderChest = true;
				lastClick.setVisible(false);
				lastClick.setClickable(false);
				levelData.getObjects().get(0).setZimage("de/acagamics/roteschlupfmuetze/objects/objects_rope.png");
				levelData.getObjects().get(0).setPosX(levelData.getPitOffsetX() + 270);
				levelData.getObjects().get(0).setPosY(levelData.getPitOffsetY() - 30);
				levelData.getObjects().get(0).scaleImage(0.5);
				levelData.getObjects().get(0).setVisible(true);
				levelData.getObjects().get(0).setClickAction("inventar");
				scriptEvent = true;
				level4animation = true;
			}
		}else if(level4droppingChest && level4moveUnderChest
				&& (player.getPlayerSprite().getX() > ((levelData.getObjects().get(1).getObjectSprite().getX() + (levelData.getObjects().get(1).getObjectSprite().getWidth())/2)) + 20
				|| player.getPlayerSprite().getX() < ((levelData.getObjects().get(1).getObjectSprite().getX() + (levelData.getObjects().get(1).getObjectSprite().getWidth())/2)) - 20)){
				//&& (pX > levelData.getPitOffsetX() + 50 + 20 
				//|| pX < levelData.getPitOffsetX() + 50 - 20 )) {
			player.moveToX((int)(levelData.getObjects().get(1).getObjectSprite().getX() + (levelData.getObjects().get(1).getObjectSprite().getWidth()/2)));
			//player.moveToX(levelData.getPitOffsetX() + 50 );
		}else if(level4droppingChest && level4moveUnderChest) {
			level4moveUnderChest = false;
			level4chestToCage = true;
		}else if(level4droppingChest && level4chestToCage) {
			levelData.getObjects().get(1).setWidth(280);
			levelData.getObjects().get(1).setHeight(200);
			levelData.getObjects().get(1).setZimage("de/acagamics/roteschlupfmuetze/objects/objects_cage.png");
			levelData.getObjects().get(1).setPosX(levelData.getObjects().get(1).getPosX()-20);
			//levelData.getObjects().get(1).setClickable(false);
			level4chestToCage = false;
			level4dropCage = true;
		}else if(level4droppingChest && level4dropCage
				&& levelData.getObjects().get(1).getObjectSprite().getY() < levelData.getPitOffsetY() - levelData.getObjects().get(1).getObjectSprite().getHeight()){
			player.moveToX(900);
			levelData.getObjects().get(1).getObjectSprite().moveY(+15);
		}else if(level4droppingChest && level4dropCage && levelData.getLevelNumber()==4){
			scriptEvent = false;
			levelData.getObjects().get(1).setClickable(true);
			levelData.getObjects().get(1).setClickAction("destroy");
			level4dropCage = false;
		}else if (level4destroy) {
			levelData.getObjects().get(1).setClickAction("build");		
			levelData.getObjects().get(1).setWidth(200);
			levelData.getObjects().get(1).setHeight(100);
			levelData.getObjects().get(1).setPosY(550);
			levelData.getObjects().get(1).setPosX(710);
			levelData.getObjects().get(1).setZimage("de/acagamics/roteschlupfmuetze/objects/objects_debris.png");
			levelData.getObjects().get(1).setClickable(true);
		}else if (level4build){
			levelData.getObjects().get(1).setWidth(110);
			levelData.getObjects().get(1).setHeight(360);
			levelData.getObjects().get(1).setPosY(280);
			levelData.getObjects().get(1).setPosX(800);
			levelData.getObjects().get(1).setZimage("de/acagamics/roteschlupfmuetze/objects/objects_ladder.png");
			scriptEvent = true;
			player.moveToX(830);
			if(player.getPlayerSprite().getX()>=800){
				level4end = true;
				level4build = false;
			}
		}else if (level4end){
			player.moveToY(95);
			if(player.getPlayerSprite().getY()<=110){
				player.moveToX(1400, true);
			}
			if(player.getPlayerSprite().getX()>=1300){
				level4end = false;
				loadLevel(1);
			}
		}
	}
		
	
}
